import 'package:development_assistant/model/column_info.dart';
import 'package:development_assistant/model/db_info.dart';
import 'package:development_assistant/model/table_info.dart';
import 'package:development_assistant/service/database_service.dart';
import 'package:get/get.dart';
class DatabaseController extends GetxController{

  @override
  void onInit() {
    super.onInit();
    // TODO: implement onInit
    getDBList();
  }

  var pickIndex=0.obs;

  var dbID=0.obs;
  var dbName="".obs;
  var dbDesc="".obs;
  var dbHost="".obs;
  var dbPort="".obs;
  var dbUrl="".obs;

  var tableID=0.obs;
  var tableName="".obs;
  var tableSort="".obs;
  var tableEngine="".obs;
  var tableDBID=0.obs;

  var columnInfo=[].obs;

  var columns=RxList<ColumnInfo>();

  var tableList=[
    {
      "table_id":null,
      "db_id":0,
      "table_name":"",
      "table_sort":"",
      "table_engine":""
    }
  ].obs;

  var columnList=[[
    {
      "column_id":null,
      "table_id":0,
      "column_name":"",
      "column_type":"",
      "column_kind":""
    }
  ].obs].obs;


  var dbInfoList=[].obs;
  var tableInfoList=[].obs;
  var columnInfoList=[].obs;

  DatabaseService databaseService=DatabaseService();

  Future addDatabase() async{
    DBInfo dbInfo=DBInfo(null, dbName.value, dbDesc.value, dbHost.value, dbPort.value, dbUrl.value);
    await databaseService.addDB(dbInfo, tableList, columnList);
    Get.snackbar("保存数据库".tr, "${'成功保存数据库'.tr} : ${dbName.value}");
    Get.offNamed("/Home",arguments: {"selectIndex":3});
  }

  Future getDBList() async{
    var resMap=await databaseService.getDBList();
    dbInfoList=resMap['dbList'];
    tableInfoList=resMap['tbList'];
    columnInfoList=resMap['colList'];
  }

  Future delDatabase() async{
    await databaseService.deleteDatabase(dbID.value);
    pickIndex.value=0;
    Get.snackbar("删除数据库".tr, "${'成功删除数据库'.tr} : ${dbName.value}");
    Get.offNamed("/Home",arguments: {"selectIndex":3});
  }

  Future updateDatabase() async{
    DBInfo dbInfo=DBInfo(dbID.value, dbName.value, dbDesc.value, dbHost.value, dbPort.value, dbUrl.value);
    await databaseService.updateDatabase(dbInfo);
    Get.snackbar("修改数据库".tr, "${'成功修改数据库'.tr} : ${dbName.value}");
    Get.offNamed("/Home",arguments: {"selectIndex":3});
  }

  Future updateTable() async{
    TableInfo tableInfo=TableInfo(tableID.value, tableDBID.value, tableName.value, tableSort.value, tableEngine.value);
    await databaseService.updateTable(tableInfo, columns);
    columns.clear();
    Get.snackbar("修改数据表".tr, "${'成功修改数据表'.tr} : ${tableName.value}");
    Get.offNamed("/Home",arguments: {"selectIndex":3});
  }

  Future delTable() async{
    await databaseService.deleteTable(tableID.value);
    columns.clear();
    Get.snackbar("删除数据表".tr, "${'成功删除数据表'.tr} : ${tableName.value}");
    Get.offNamed("/Home",arguments: {"selectIndex":3});
  }

  Future addTable() async{
    TableInfo tableInfo=TableInfo(null, tableDBID.value, tableName.value, tableSort.value, tableEngine.value);
    await databaseService.addTable(tableInfo, columns);
    columns.clear();
    Get.snackbar("添加数据表".tr, "${"成功添加数据表".tr} : ${tableName.value}");
    Get.offNamed("/Home",arguments: {"selectIndex":3});
  }


}